package com.apexsoft.aas.service.model;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Class 请求包
 *
 * @author lowezheng
 * @version 1.0.0, 19/01/30
 */
@Deprecated
public class AUploadResponse implements Serializable {
    /**
     * Field  状态码
     * <p>
     * >0 ,成功
     * <0 ,失败
     */
    private Integer code;

    /**
     * Field  响应说明
     */
    private String note;
    /**
     * Field  上传后返回的文件码
     */
    private String filecode;
    /**
     * Field  错误码
     */
    private Integer errcode =0;

    /**
     * Field  耗时(毫秒)
     */
    private String duration;

    /**
     * Field  结果对象
     */
    private Map<String, Object> data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getFilecode() {
        return filecode;
    }

    public void setFilecode(String filecode) {
        this.filecode = filecode;
    }

    public Integer getErrcode() {
        return errcode;
    }

    public void setErrcode(Integer errcode) {
        this.errcode = errcode;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    public void addData(String key, Object value) {
        if (data == null) {
            data = new HashMap<>();
        }
        data.put(key, value);
    }

    public void removeData(String key) {
        if (data != null) {
            data.remove(key);
        }
    }

    public <T> T getDataEx(String key, Class<T> cls) {
        if (data != null) {
            Object o = data.get(key);
            //防止JSONObject转成自己
            if (o == null) {
                return null;
            } else if (o instanceof JSONObject && cls != JSONObject.class) {
                return ((JSONObject) o).toJavaObject(cls);
            } else if (o.getClass() == cls) {
                return (T) o;
            } else {
                throw new RuntimeException("key value class: " + o.getClass().getName() + ", not match target class:" + cls.getName());
            }

        } else {
            return null;
        }

    }

    public <T> T getDataEx(Class<T> cls) {
        return (T) new JSONObject(data).toJavaObject(cls);
    }

}

